package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.CartMapper;
import com.fantastic.mapper.zjs.OrderDetailsMapper;
import com.fantastic.mapper.zjs.OrdersMapper;
import com.fantastic.mapper.zjs.UserMapper;
import com.fantastic.model.pojo.zjs.Cart;
import com.fantastic.model.pojo.zjs.OrderDetails;
import com.fantastic.model.pojo.zjs.Orders;
import com.fantastic.model.vo.zjs.CartVo;
import com.fantastic.service.zjs.OrderDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/6 10:46
 * @description: 订单详情信息表
 */
@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Resource
    private OrderDetailsMapper orderDetailsMapper;
    @Resource
    private CartMapper cartMapper;

    /**
     * 查询订单详情信息
     *
     * @param orderDetails
     * @return
     */
    @Override
    public List<OrderDetails> listOrderDetails(OrderDetails orderDetails) {
        List<OrderDetails> orderDetailsList = orderDetailsMapper.listOrderDetails(orderDetails);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), orderDetailsList);
        return orderDetailsList;
    }

    /**
     * 查询单个
     *
     * @param orderDetailsId
     * @return
     */
    @Override
    public OrderDetails getOrderDetails(Integer orderDetailsId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), orderDetailsId);
        return orderDetailsMapper.getOrderDetails(orderDetailsId);
    }

    /**
     * 添加订单详情信息
     *
     * @param orderDetails
     * @return
     */
    @Override
    public Integer insertOrderDetails(OrderDetails orderDetails) {
        if (orderDetails.getVarietyId() != null || orderDetails.getMealId() != null) {
            IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), orderDetails.getOrderId());
            IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                    orderDetails.getOrderDetailsCount());
            IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                    orderDetails.getOrderDetailsRemark());
            IsEmptyUtils.objectIsNull(new InsertException(RespStatusInfo.DATA_ERROR),
                    orderDetails.getOrderDetailsPrice());
        } else {
            new InsertException(RespStatusInfo.DATA_ERROR);
        }
        return orderDetailsMapper.insertOrderDetails(orderDetails);
    }

    /**
     * 批量添加订单详情信息
     *
     * @param orders 订单对象
     * @return
     */
    @Override
    @Transactional
    public Integer addsOrderDetails(Orders orders) {
        //通过下单的账号id拿到购物车的所有数据
        List<CartVo> cartVos = cartMapper.listCartVo(orders.getAccountId());
        //没数据抛出购物车为空状态
        IsEmptyUtils.listIsEmpty(new InsertException(RespStatusInfo.CART_NOT), cartVos);
        //将购物车的数据添加到订单详情中
        for (CartVo cart : cartVos) {
            OrderDetails oDetails = new OrderDetails();
            //填充订单编号
            oDetails.setOrderId(orders.getOrderId());
            //填充菜品id 或是套餐id
            if (cart.getMeal() != null && cart.getMeal().getMealId() != null) {
                oDetails.setMealId(cart.getMeal().getMealId());
            }
            if (cart.getVariety() != null && cart.getVariety().getVarietyId() != null) {
                oDetails.setVarietyId(cart.getVariety().getVarietyId());
            }
            //填充规格备注
            oDetails.setOrderDetailsRemark(cart.getCartSpec());
            //填充价格
            oDetails.setOrderDetailsPrice(cart.getCartPrice());
            //填充数量
            oDetails.setOrderDetailsCount(cart.getCartCount());
            //执行将购物车信息添加到订单详情
            orderDetailsMapper.insertOrderDetails(oDetails);
        }
        //执行将购物车信息进行删除
        Cart c = new Cart();
        c.setAccountId(orders.getAccountId());
        return cartMapper.deleteCart(c);
    }

    /**
     * 修改订单详情信息
     *
     * @param orderDetails
     * @return
     */
    @Override
    public Integer updateOrderDetails(OrderDetails orderDetails) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                orderDetails.getOrderDetailsId());
        return orderDetailsMapper.updateOrderDetails(orderDetails);
    }

    /**
     * 删除订单详情信息
     *
     * @param orderDetailsId
     * @return
     */
    @Override
    public Integer deleteOrderDetails(Integer orderDetailsId) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                orderDetailsId);
        return orderDetailsMapper.deleteOrderDetails(orderDetailsId);
    }

}
